package jpa.service.synchro.staging;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.AGenCode;
import jpa.entitys.synchro.staging.AGenType;
import jpa.service.synchro.common.JPAService;

public class AGenCodeServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(AGenCodeServices.class);

	public AGenCodeServices(EntityManager em) {
		super(em);
	}
	
	public AGenCode findById(String id) throws Exception {		
		
		return super.ConvertToYoureObject(super.find(id, "AGenCode.findById"));
	}
	
	public AGenCode Updated(AGenCode value) throws Exception {
		if(value != null)
		{
			value.setUpdatedBy("PROC-QUEUE");
			value.setUpdatedOn(this.getCurrent_date());
			return (AGenCode) super.updated(value);
		}else
			return null;
	}
	
	public AGenCode findByCode(BigDecimal aGenTypeId, String code) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("aGenTypeId", aGenTypeId);
		mapWhereValue.put("code", code);
		
		List<AGenCode> lst = super.ConvertToYoureObject(super.find("AGenCode.findByCode", mapWhereValue));		
		if(CommonService.getSize(lst) == 1)
			return lst.get(0);
		else 
			throw new Exception("Errpr AGenCode have more than one record(s).");
	}
	
	public AGenCode Inserted(AGenCode vlaue) throws Exception {
		AGenCode obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getAGenTypeId()))
			{
				String id = super.SequenceNext("A_GEN_CODE");					
				obj.setAGenTypeId(CommonService.ConvertStringToBigDecimal(id));
				obj.setCreatedBy("PROC-QUEUE");
				obj.setCreatedOn(this.getCurrent_date());
				obj.setUpdatedBy("PROC-QUEUE");
				obj.setUpdatedOn(this.getCurrent_date());
								
				super.inserted(obj);
			}
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return obj;
	}
	
	public List<AGenCode> Inserted(List<AGenCode> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(AGenCode obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getAGenTypeId()))
				{
					String id = super.SequenceNext("A_GEN_CODE");					
					obj.setAGenTypeId(CommonService.ConvertStringToBigDecimal(id));
					obj.setCreatedBy("PROC-QUEUE");
					obj.setCreatedOn(this.getCurrent_date());
					obj.setUpdatedBy("PROC-QUEUE");
					obj.setUpdatedOn(this.getCurrent_date());
					common_lst.add(obj);
				}	
			}
			super.inserted(common_lst);
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}
	
	public List<AGenCode> Updated(List<AGenCode> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(AGenCode obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getAGenTypeId()))
				{	obj.setUpdatedBy("PROC-QUEUE");
					obj.setUpdatedOn(this.getCurrent_date());
					common_lst.add(obj);
				}
			}
			super.updated(common_lst);
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}

}
